package com.hy.mydesktop.server.dao;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.spi.PersistenceUnitInfo;

import net.sf.gilead.configuration.ConfigurationHelper;
import net.sf.gilead.core.PersistentBeanManager;
import net.sf.gilead.core.hibernate.jpa.HibernateJpaUtil;

import org.eclipse.jdt.internal.compiler.ast.SuperReference;
import org.hibernate.cfg.Configuration;
import org.hibernate.ejb.Ejb3Configuration;
import org.hibernate.ejb.HibernatePersistence;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.google.gwt.dev.util.collect.HashMap;
import com.hy.enterprise.framework.persistence.api.MyAbstractDao;
import com.hy.enterprise.framework.util.lang.UUIDUtil;
import com.hy.mydesktop.shared.persistence.domain.User;
import com.hy.mydesktop.shared.persistence.domain.util.Persistable;

@Component("userDao")
public class UserDaoImpl<PersistentEntity extends Persistable> extends MyAbstractDao<User > implements UserDao<User>{
	
	/*EntityManagerFactory entityManagerFactory = null;
	
	public EntityManagerFactory getEntityManagerFactory() {
		return entityManagerFactory;
	}

	@Resource
	public void setEntityManagerFactory(EntityManagerFactory entityManagerFactory) {
		this.entityManagerFactory = entityManagerFactory;
	}*/

	
	public User isUser(User user) {
//		EntityManager entityManager = getEntityManagerFactory().createEntityManager();
		EntityManager entityManager = getEntityManager();

		//ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		//entityManagerFactory = context.getBean("entityManagerFactory", EntityManagerFactory.class);

		//entityManagerFactory = Persistence.createEntityManagerFactory("mydesktopJpa");
		//entityManagerFactory = new HibernatePersistence().createEntityManagerFactory("mydesktopJpa",null);
		//EntityManager entityManager = entityManagerFactory.createEntityManager();
		System.out.println(getEntityManagerFactory()+"aaaaaaaaaaaaaaaaaaaaaaaa");
		System.out.println(getEntityManager()+"aaaaaaaaaaaaaaaaaaaaaaaa");
		
		//entityManager.getTransaction().begin();
		Query query = entityManager.createQuery("select u from User as u where u.id =?1");
		query.setParameter(1, "u1");
		Object object= query.getSingleResult();
		User user2 = new User();
		user2.setId(UUIDUtil.randomUUID());
		user2.setName("sdfsdf");
		entityManager.persist(user2);
		//entityManager.getTransaction().commit();
		//entityManager.close();
		
		return (User)object;
		
	}

	
	//@Transactional
	public Boolean addUser(User user) {
		
//		PersistentBeanManager beanManager = ConfigurationHelper.initStatelessBeanManager(new HibernateJpaUtil(getEntityManagerFactory()));
		PersistentBeanManager beanManager = ConfigurationHelper.initStatelessBeanManager(new HibernateJpaUtil(getEntityManagerFactory()));

		/*MyBean cloneBean = (MyBean) beanManager.clone(myEntity);

		// Send cloneBean, change it, do what you want...

		myEntity = (MyBean) beanManager.merge(cloneBean);

		entityDAO.save(myEntity);*/
		//EntityManager entityManager = getEntityManagerFactory().createEntityManager();
		//getEntityManager().persist(user);
		System.err.println(getEntityManager());
		//persist(user);
		return true;
	}

	

	

}
